//Network Assignment client part
//Richard Allsopp
//A Simple Java Client 

//importing java tools
import java.io.*;
import java.net.*;
import java.util.*;

public class testClient {
    public static void main(String[] args) throws IOException { //throwing the usual exception
        
        Socket mySock = null;               //socket for connection
        PrintWriter out = null;             //printwriter for sending strings
        BufferedReader in = null;           //bufferedReader for reading strings
        
        Scanner serverName = new Scanner(System.in);
        System.out.println("Enter the Server name: ");
        String serName = serverName.next();
        
        Scanner serverPort = new Scanner(System.in);
        System.out.println("Enter the Port number: ");
        int serPort = serverPort.nextInt();

        try {           //try catch block for catching the can't find server or can't connect to server exception
            mySock = new Socket(serName, serPort);     //port number
            out = new PrintWriter(mySock.getOutputStream(), true);      //for sending strings
            in = new BufferedReader(new InputStreamReader(mySock.getInputStream()));    //for reading strings
        } catch (UnknownHostException e) {
            System.err.println("Can't find " + serName);        //can't find exception
            System.exit(1);     //close the program if exception
        } catch (IOException e) {
            System.err.println("Can't connect to port number " + serPort);   //can't connect exception
            System.exit(1);     //close the program if exception
        }
        BufferedReader myWriter = new BufferedReader(new InputStreamReader(System.in)); 
        String serverMessage;           //variables for server response and client response
        String userMessage;
        String searchSelection;
        
        Scanner scan = new Scanner(System.in);
        menu(1);
        while(scan.hasNext())
        {   
            int input = scan.nextInt();
            if(input == 1)
            {
                Scanner scanLogReg = new Scanner(System.in);
                System.out.println("Enter your name:");
                String logDetReg = scanLogReg.next();
                System.out.println("Enter your ID number:");
                String logDetRegAdd = scanLogReg.next();
                userMessage = "0|L|"+logDetReg+"|"+logDetRegAdd+"|%";
                out.println(userMessage);
                System.out.println(userMessage);
                serverMessage = in.readLine();
                System.out.println(serverMessage);
                if(serverMessage.charAt(0) == '1')
                {
                    menu(2);
                }
                else
                {
                    menu(4);
                }   
            }
            if(input == 2)
            {
                System.out.println("You can search EBooks via these fields:");
                System.out.println("A for author");
                System.out.println("T for title");
                System.out.println("C for category");
                System.out.println("P for price");
                System.out.println("I for ID");
                System.out.println("Type all to return all EBooks.");
                System.out.println();
                System.out.println("Type a letter to begin search");
                Scanner search = new Scanner(System.in);
                System.out.println("Enter your choice:");
                searchSelection = search.next();
                userMessage = "1|S|"+searchSelection+"|%";
                out.println(userMessage);
                System.out.println(userMessage);
                serverMessage = in.readLine();
                System.out.println(serverMessage);
                menu(6);
            }
            if(input == 4)
            {
                userMessage = "Terminate";
                out.println(userMessage);
                break;
            }
            if(input == 5)
            {
                Scanner scanReg = new Scanner(System.in);
                System.out.println("Enter your name:");
                String detReg = scanReg.next();
                System.out.println("Enter your Address:");
                String regAdd = scanReg.next();
                userMessage = "0|R|"+detReg+"|"+regAdd+"|%";
                out.println(userMessage);
                System.out.println(userMessage);
                serverMessage = in.readLine();
                System.out.println(serverMessage);
                if(serverMessage.charAt(0) == '1')
                {
                    menu(2);
                }
                else
                {
                    System.out.println("Registration attempt failed, please try again.");
                    menu(4);
                }
            }
        }
        out.close();
        in.close();

        mySock.close();
        
    }
    public static int menu(int selection)
    {
        if(selection == 1)
        {
            System.out.println();
            System.out.println("You are not logged in to the Test book system");
            System.out.println();
            System.out.println("1. login");
            System.out.println("5. Register");
            System.out.println("4. Disconnect");
        }
        if(selection == 2)
        {
            System.out.println();
            System.out.println("You are logged in to the Test book system");
            System.out.println();
            System.out.println("2. Search EBooks");
            System.out.println("3. Buy EBook");
            System.out.println("4. Disconnect");
        }
        if(selection == 3)
        {
            System.out.println();
            System.out.println("You are logged in to the Test book system");
            System.out.println();
            System.out.println("Author");
            System.out.println("Title");
            System.out.println("Price");
        }
        if(selection == 4)
        {
            System.out.println();
            System.out.println("You are not logged in to the Test book system");
            System.out.println();
            System.out.println("5. Register");
            System.out.println("4. Disconnect");
        }
        if(selection == 6)
        {
            System.out.println();
            System.out.println("2. Search EBooks again");
            System.out.println("3. Buy EBook");
            System.out.println("4. Disconnect");
        }   
        return selection;
    }
    
}           